﻿#include "pch.h"
#if _QG_USE_GL
#include "qgdrv_gl_rdh.h"

#if QG_OPENGL_ES2

//
#define QGES2_IMPL_ENTRYPOINT(type,func)	type func = NULL;
#define QGES2_TEST_ENTRYPOINT(type,func)	if (func == NULL) return false;
#define QGES2_PRINT_ENTRYPOINT(type,func)	k_mesgf("%s -> %s\n", K_STRING(func), func!=NULL ? "ok" : "fail");
#define QGES2_GET_PROC_ADDRESS(type,func)	func = (type)eglGetProcAddress(K_STRING(func));

//
QGES2_ENTRYPOINT_LIST(QGES2_IMPL_ENTRYPOINT);
QGES2_ENTRYPOINT_LIST_OCCLUSION(QGES2_IMPL_ENTRYPOINT);

//
bool qbOpenGLES2::qgl_support_atitc = false;
bool qbOpenGLES2::qgl_support_bgra8888 = false;
bool qbOpenGLES2::qgl_support_occlusion = false;
bool qbOpenGLES2::qgl_support_color_buffer_half_float = false;
bool qbOpenGLES2::qgl_support_srgb = false;
bool qbOpenGLES2::qgl_support_s3tc = false;
bool qbOpenGLES2::qgl_support_pvrtc = false;
bool qbOpenGLES2::qgl_support_depth_texture = false;
bool qbOpenGLES2::qgl_support_map_buffer = false;
bool qbOpenGLES2::qgl_support_rgba8 = false;
bool qbOpenGLES2::qgl_support_half_float = false;
bool qbOpenGLES2::qgl_support_texture_float = false;
bool qbOpenGLES2::qgl_support_texture_half_float = false;
bool qbOpenGLES2::qgl_support_texture_npot = false;
bool qbOpenGLES2::qgl_support_packed_depth_stencil = false;

//
void qbOpenGLES2::QglInitializeExtensions(const char* extstr)
{
	qbOpenGLBase::QglInitializeExtensions(extstr);

	//
	qgl_support_atitc = k_stristr(extstr, "GL_AMD_COMPRESSED_ATC_TEXTURE") != NULL || k_stristr(extstr, "GL_ATI_TEXTURE_COMPRESSION_ATITC") != NULL;
	qgl_support_bgra8888 = k_stristr(extstr, "GL_APPLE_TEXTURE_FORMAT_BGRA8888") != NULL;
	qgl_support_occlusion = k_stristr(extstr, "GL_ARB_OCCLUSION_QUERY2") != NULL || k_stristr(extstr, "GL_EXT_OCCLUSION_QUERY_BOOLEAN") != NULL;
	qgl_support_color_buffer_half_float = k_stristr(extstr, "GL_EXT_COLOR_BUFFER_HALF_FLOAT") != NULL;
#if 0
	qgl_support_srgb = k_stristr(extstr, "GL_EXT_SRGB") != NULL;
#else
	// IOS7에서 안됨
	qgl_support_srgb = false;
#endif
	qgl_support_s3tc = k_stristr(extstr, "GL_EXT_TEXTURE_COMPRESSION_S3TC") != NULL || k_stristr(extstr, "GL_NV_TEXTURE_COMPRESSION_S3TC") != NULL;
	qgl_support_pvrtc = k_stristr(extstr, "GL_IMG_TEXTURE_COMPRESSION_PVRTC") != NULL;
	qgl_support_depth_texture = k_stristr(extstr, "GL_OES_DEPTH_TEXTURE") != NULL;
	qgl_support_map_buffer = k_stristr(extstr, "GL_OES_MAPBUFFER") != NULL;
	qgl_support_rgba8 = k_stristr(extstr, "GL_OES_RGB8_RGBA8") != NULL;
	qgl_support_half_float = k_stristr(extstr, "GL_OES_VERTEX_HALF_FLOAT") != NULL;
	qgl_support_texture_float = k_stristr(extstr, "GL_OES_TEXTURE_FLOAT") != NULL;
	qgl_support_texture_half_float = k_stristr(extstr, "GL_OES_TEXTURE_HALF_FLOAT") != NULL;
	qgl_support_texture_npot = k_stristr(extstr, "GL_OES_TEXTURE_NPOT") != NULL;
	qgl_support_packed_depth_stencil = k_stristr(extstr, "GL_OES_PACKED_DEPTH_STENCIL") != NULL;

	//
	static bool sInitFunc = false;

	if (!sInitFunc)
	{
		sInitFunc = true;

		// 공통
		QGES2_ENTRYPOINT_LIST(QGES2_GET_PROC_ADDRESS);

		if (qgl_support_occlusion)
		{
			// 오클루전 전용
			QGES2_ENTRYPOINT_LIST_OCCLUSION(QGES2_GET_PROC_ADDRESS);
		}
	}

	// 카파
	kany a;

	// 버전
	a.cp = glGetString(GL_VERSION);

	if (k_strnicmp(a.s, "OPENGLES", 8) == 0)
		a.f = k_atof((const char*)a.s + 9);
	else if (k_strnicmp(a.s, "OPENGL ES", 9) == 0)
		a.f = k_atof((const char*)a.s + 10);
	else
		a.f = k_atof((const char*)a.s);

	qgl_version = (kushort)(k_floor(a.f) * 100.0f + k_ceil(k_fract(a.f) * 10.0f));

	// 세이더 버전
	a.cp = glGetString(GL_SHADING_LANGUAGE_VERSION);

	if (k_strnicmp(a.s, "OPENGL ES GLSL ES ", 18) == 0)
		a.f = k_atof((const char*)a.cp + 18);
	else if (k_strnicmp(a.s, "OPENGL ES GLSL ", 15) == 0)
		a.f = k_atof((const char*)a.cp + 15);
	else
		a.f = k_atof((const char*)a.cp);

	qgl_shader_version = (kushort)(k_floor(a.f) * 100.0f + k_ceil(k_fract(a.f) * 10.0f));
}

//
bool qbOpenGLES2::QglTestExtensions()
{
	if (!qbOpenGLBase::QglTestExtensions())
		return false;

#if 0 || _SB_DEBUG_
	QGES2_ENTRYPOINT_LIST(QGES2_PRINT_ENTRYPOINT);
	QGES2_ENTRYPOINT_LIST_OCCLUSION(QGES2_PRINT_ENTRYPOINT);
#endif

	QGES2_ENTRYPOINT_LIST(QGES2_TEST_ENTRYPOINT);
	QGES2_ENTRYPOINT_LIST_OCCLUSION(QGES2_TEST_ENTRYPOINT);

	return true;
}

#endif	// QG_OPENGL_ES2

#endif	// _QG_USE_GL
